<template>
  <!-- 表单 -->
  <el-form
    ref="form"
    label-width="120px"
    :model="formData"
    :rules="formDataRules"
  >
    <el-form-item label="姓名" prop="username">
      <el-input
        v-model="formData.username"
        style="width: 90%"
        placeholder="请输入姓名"
      />
    </el-form-item>
    <el-form-item label="手机" prop="mobile">
      <el-input
        v-model="formData.mobile"
        style="width: 90%"
        placeholder="请输入手机号"
      />
    </el-form-item>
    <el-form-item label="入职时间" prop="timeOfEntry">
      <el-date-picker
        v-model="formData.timeOfEntry"
        style="width: 90%"
        placeholder="请选择入职时间"
      />
    </el-form-item>
    <el-form-item label="聘用形式" prop="formOfEmployment">
      <el-select
        v-model="formData.formOfEmployment"
        style="width: 90%"
        placeholder="请选择"
      >
        <el-option
          v-for="item in hireType"
          :key="item.id"
          :value="item.id"
          :label="item.value"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="工号" prop="workNumber">
      <el-input
        v-model="formData.workNumber"
        style="width: 90%"
        placeholder="请输入工号"
      />
    </el-form-item>
    <el-form-item label="部门" prop="departmentName">
      <el-input
        v-model="formData.departmentName"
        style="width: 90%"
        placeholder="请选择部门"
        @focus="focusFn"
      />
      <div v-if="ShowTreeBox">
        <el-tree
          :data="treeData"
          :props="{ label: 'name' }"
          default-expand-all
          @node-click="treeClickFn"
        />
      </div>
    </el-form-item>
    <el-form-item label="转正时间">
      <el-date-picker
        v-model="formData.correctionTime"
        style="width: 90%"
        placeholder="请选择转正时间"
      />
    </el-form-item>
    <el-form-item>
      <el-button @click="cancelAdd">取消</el-button>
      <el-button type="primary" @click="addSubmit">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { addEmployeeAPI } from '@/api/employees'
import EmployeeEnum from '@/constant/employees'
import { getDepartments } from '@/api/departments'
import { getTreeData } from '@/utils/getTreeData'

export default {
  name: 'EmpDialog',
  data() {
    return {
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      formDataRules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      },
      //   引用枚举hireType对象的属性
      hireType: EmployeeEnum.hireType,
      treeData: [],
      ShowTreeBox: false
    }
  },
  methods: {
    // 重置表单的方法
    resetForm() {
      // 清空表单数据
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      // 重置表单
      this.$refs.form.resetFields()
    },
    //   部门名称获得焦点，显示树形结构
    focusFn() {
      this.getDepartments()
    },
    // 点击树形结构数据
    treeClickFn(data) {
      // data为当前选中的部门对象
      // 如果有子部门，则不能被选中
      if (data && data.children) {
        return
      }
      this.formData.departmentName = data.name
      //   隐藏树形结构
      this.ShowTreeBox = false
    },

    // 获取组织架构数据
    async getDepartments() {
      const res = await getDepartments()
      //   console.log(res)
      this.treeData = getTreeData(res.data.depts)
      this.ShowTreeBox = true
    },

    //   点击取消
    cancelAdd() {
      this.$emit('colseEvent')
    },

    // 点击确定
    async addSubmit() {
      // 兜底验证
      this.$refs.form.validate(async valid => {
        if (!valid) return
      })
      const res = await addEmployeeAPI(this.formData)
      console.log(res)
      this.$message.success(res.message)
      //   关闭弹框
      this.$emit('colseEvent')
      //   重新获取员工列表
      this.$emit('updateList')
    }
  }
}
</script>

<style lang="scss" scoped></style>
